<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <style>
    html,
    body,
    #container {
      width: 800px;
      height: 800px;
    }
    </style>
    <title>多边形编辑器吸附功能</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://webapi.amap.com/maps?v=2.0&key=ae82f1373cedbca373e78dba0188b6b9&plugin=AMap.PolygonEditor"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
</head>
<body>
<div id="container"></div>
<div>
    <button class="btn" onclick="createPolygon()" style="margin-bottom: 5px">新建</button>
    <button class="btn" onclick="polyEditor.open()" style="margin-bottom: 5px">开始编辑</button>
    <button class="btn" onclick="polyEditor.close()">结束编辑</button>
</div>
<script type="text/javascript">
    var map = new AMap.Map("container", {
      center: [116.471354, 39.994257],
      zoom: 16.8
    });

    map.setFitView();
    var polyEditor = new AMap.PolygonEditor(map);

    polyEditor.on('add', function (data) {
      var polygon = data.target;
      polyEditor.addAdsorbPolygons(polygon);
      polygon.on('dblclick', () => {
        polyEditor.setTarget(polygon);
        polyEditor.open();
      })
      console.log(data.obj.getPath());
    })

    function createPolygon() {
      polyEditor.close();
      polyEditor.setTarget();
      polyEditor.open();
    }
  </script>
</body>
</html>